<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\jia;

use app\store\controller\Controller;
use app\store\model\jia\Area as AreaModel;
use cores\exception\BaseException;
use think\response\Json;

use think\facade\Db;

/**
 * 商品管理控制器
 * Class Goods
 * @package app\store\controller
 */
class Area extends Controller {
	/**
	 * 商品列表
	 * @return Json
	 * @throws \think\db\exception\DbException
	 */
	public function list(): Json {
		// 获取列表记录
		$model = new AreaModel;
		$param = $this->request->param();

		$list = $model->getList($param);
		return $this->renderSuccess(compact('list'));
	}
	public function floorList(): Json {
		// 获取列表记录
		$model = new AreaModel;
		$param = $this->request->param();
		$floor = $param['floor'];
		$list = [];

		if ($floor > 0) {
			for ($i = 0; $i < $floor; $i++) {
				$num = $i + 1;
				$list[] = ['label' => $num . '层', 'value' => $num];
			}

		}

		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 根据商品ID集获取列表记录
	 * @param array $buildIds
	 * @return Json
	 */
	public function listByIds(array $buildIds): Json {
		// 获取列表记录
		$model = new AreaModel;
		$list = $model->getListByIds($buildIds);
		return $this->renderSuccess(compact('list'));
	}


	/**
     * 获取详情记录
     * @param int $shopId
     * @return Json
     */
    public function detail(int $id): Json
    {
        // 获取门店详情
        $detail = AreaModel::detail($id);
        // 获取logoImage (这里不能用with因为编辑页需要logoImage对象)
        $detail['floors']=Db::name('jia_manage_floor')->field('floor')->where('log_id',$detail['id'])->select()->toArray();
        //!empty($detail) && $detail['floors'];
        return $this->renderSuccess(compact('detail'));
    }

	/**
	 * 商品详情(基础信息)
	 * @param int $buildId
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function basic(int $buildId): Json {
		// 获取商品详情
		$model = new AreaModel;
		$detail = $model->getBasic($buildId);
		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 添加商品
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function add(): Json {
		$model = new AreaModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * 商品编辑
	 * @param int $buildId
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function edit(int $id): Json {
		// 商品详情
		$model = AreaModel::detail($id);
		// 更新记录
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	/**
	 * 修改商品状态(上下架)
	 * @param array $buildIds 商品id集
	 * @param bool $state 为true表示上架
	 * @return Json
	 */
	public function state(array $buildIds, bool $state): Json {
		$model = new AreaModel;
		if (!$model->setStatus($buildIds, $state)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}

	/**
	 * 删除商品
	 * @param array $buildIds
	 * @return Json
	 */
	public function delete(int $id): Json {

		$model = new AreaModel;
		if (!$model->remove($id)) {
			return $this->renderError($model->getError() ?: '删除失败');
		}
		return $this->renderSuccess('删除成功');
	}
}
